const ModelSongList = require('../../models/songlist'),
    ModelMusic = require('../../models/music'),
    ComTool = require('../../utils/commonTools'),
    FileTool = require('../../utils/filetool');
const imgSavePath = "/images/songlist_cover_src";
// 删除歌单
exports.delSongList = (req, res, next) => {
    ModelSongList.delSongList(req.query, (value) => {
        res.render('success', {
            data: JSON.stringify({
                message: "删除成功",
                status: 200,
                affectedRows: value.affectedRows
            })
        })
    }, (error) => {
        res.render('fail', {
            data: JSON.stringify({
                message: "删除失败",
                status: 200,
                error
            })
        })
    });
}
// 编辑歌单
exports.updateSongList = async (req, res, next) => {

    const {
        data,
        _imgfile
    } = req.body;
    let fileImg = _imgfile ? await FileTool.saveImage(_imgfile, './public' + imgSavePath) : null;

    if (fileImg === false) {
        return res.render('fail', {
            data: JSON.stringify({
                message: "图片保存失败",
                status: 200,
            })
        })
    } else if (fileImg !== null) {
        if (data.list_pic && data.list_pic_name)
            FileTool.removeFile('./public' + data.list_pic, data.list_pic_name);
    }

    let newData = {
        ...data,
        list_pic_name: fileImg ? fileImg.name : null,
        list_pic: fileImg ? imgSavePath + '/' + fileImg.saveFloder + '/' : null,
    }

    const {
        list_id
    } = newData;

    if (list_id) {
        delete newData.list_id;
        ModelSongList.updateSongList(newData, {
            list_id
        }, (value) => {
            res.render('success', {
                data: JSON.stringify({
                    message: "更新成功",
                    status: 200,
                    affectedRows: value.affectedRows
                })
            })
        }, (error) => {
            res.render('fail', {
                data: JSON.stringify({
                    message: "更新失败",
                    status: 200,
                    error
                })
            })
        });
    }
}
/** 添加歌单 */
exports.addSongList = (req, res, next) => {
    // const { music_name } = req.body;
    ModelSongList.addSongList({
            ...req.body,
            list_id: ComTool.orderCode()
        },
        (value) => {
            res.render('success', {
                data: JSON.stringify({
                    message: "添加成功",
                    status: 200,
                    affectedRows: value.affectedRows
                })
            })
        }, (error) => {
            res.render('fail', {
                data: JSON.stringify({
                    message: "添加失败",
                    status: 200,
                    error
                })
            })
        });
}

/** 根据歌单id查找歌单详情 */
exports.findSongListDetail = async (req, res, next) => {
    let {
        list_id
    } = req.body;

    let SongList = await ModelSongList.findSongList({
        list_id
    });
    SongList.success(async (listData) => {
        if (listData.length) {
            let music_ids = listData[0].music_ids;
            let Music = await ModelMusic.findMusicByids(music_ids);
            Music.success((music) => {
                if (music.length) {
                    res.render('success', {
                        data: JSON.stringify({
                            message: "查找到音乐",
                            value: {
                                playlist: listData[0],
                                privileges: {
                                    songlist: music,
                                    length: music.length
                                }
                            },
                            status: 200,
                        })
                    })
                } else {
                    res.render('success', {
                        data: JSON.stringify({
                            message: "没有找到音乐",
                            value: {
                                playlist: listData[0],
                                privileges: {
                                    songlist: music,
                                    length: music.length
                                }
                            },
                            status: 200,
                        })
                    })
                }
            });
        } else {
            res.render('fail', {
                data: JSON.stringify({
                    message: "没有找到歌单",
                    value: value,
                    length: value.length
                })
            })
        }
    });

    SongList.fail((error) => {
        res.render('fail', {
            data: JSON.stringify({
                message: "查找失败",
                error
            })
        })
    })
}

//随机查找8个歌单
exports.findRand = (req, res, next) => {
    ModelSongList.findRand(
        (value) => {
            res.render('success', {
                data: JSON.stringify({
                    message: "查找成功",
                    status: 200,
                    value: value,
                    length: value.length
                })
            })
        }, (error) => {
            res.render('fail', {
                data: JSON.stringify({
                    message: "查找失败",
                    status: 200,
                    error
                })
            })
        });
}

/** 查找歌单 */
exports.findSongList = async (req, res, next) => {
    let {
        condition,
        cat,
        tag,
        order,
        limit,
        offset,
    } = req.body;

    if (tag == '0') {
        tag = ""
    }

    let SongList = await ModelSongList.findSongList(condition, tag, offset, limit);
    SongList.success((value) => {
        if (value.length) {
            res.render('success', {
                data: JSON.stringify({
                    message: "查找到歌单",
                    value: {
                        playlists: value,
                        total: value.length
                    },
                    status: 200,
                    length: value.length
                })
            })
        } else {
            res.render('fail', {
                data: JSON.stringify({
                    message: "没有找到歌单",
                    value: value,
                    length: value.length
                })
            })
        }
    });

    SongList.fail((error) => {
        res.render('fail', {
            data: JSON.stringify({
                message: "查找失败",
                error
            })
        })
    })
}
//查找所有歌单
exports.list = (req, res, next) => {
    let params = req.body;
    ModelSongList.findAll(
        (value) => {
            value.map((item, index) => {
                if (item.music_ids && item.music_ids != '(Null)') {
                    item.music_ids = item.music_ids.split(',');
                } else
                    item.music_ids = [];

                if (item.lable_ids)
                    item.lable_ids = item.lable_ids.split(',');
                if (item.tags) {
                    item.tags = item.tags.split(',');
                }
            })

            res.render('success', {
                data: JSON.stringify({
                    message: "查找成功",
                    status: 200,
                    value: value,
                    length: value.length
                })
            })
        }, (error) => {
            res.render('fail', {
                data: JSON.stringify({
                    message: "查找失败",
                    status: 200,
                    error
                })
            })
        });
}